package com.android.mios.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.android.mios.R;
import com.android.mios.model.Device;
import com.android.mios.util.MyGridView;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class AllDeviceListAdapter extends BaseExpandableListAdapter {
	private ArrayList<HashMap<String, Object>> groupList;
	ArrayList<List<Device>> childList;
	private LayoutInflater mInflater;
	private MyGridView toolbarGrid;
	private Activity mActivity;
	public static int light = 0;
	public static int curtain = 1;
	public static int lock = 2;
	public static int sensors = 3;
	public static int climate = 4;

	public AllDeviceListAdapter(Activity mActivity,
			ArrayList<HashMap<String, Object>> groupList,
			ArrayList<List<Device>> childList, int resource) {
		this.groupList = groupList;
		this.childList = childList;
		this.mActivity = mActivity;
		mInflater = LayoutInflater.from(mActivity);
	//	Log.i("AllDeviceListAdapter===========create", this.groupList.size()+"======="+this.childList.size()+"");
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return groupList.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		//return childList.size();
		return 1;
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return groupList.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childList.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		//Log.i("AllDeviceListAdapter===========", this.groupList.size()+"=\\=\\\\"+this.childList.get(groupPosition).size()+"");
		if (convertView == null)
			convertView = mInflater.inflate(R.layout.grouplayout, null);

		ImageView iv_device = (ImageView) convertView
				.findViewById(R.id.iv_device);
		TextView txt_name = (TextView) convertView.findViewById(R.id.groupName);
		TextView txt_count = (TextView) convertView
				.findViewById(R.id.groupCount);

		int category = (Integer) groupList.get(groupPosition).get("category");

		if (category == light)
			iv_device.setBackgroundResource(R.drawable.light_icon);
		else if (category == curtain)
			iv_device.setBackgroundResource(R.drawable.curtain_icon);
		else if (category == lock)
			iv_device.setBackgroundResource(R.drawable.lock_icon);
		else if (category == climate)
			iv_device.setBackgroundResource(R.drawable.climate_icon);
		else if (category == sensors)
			iv_device.setBackgroundResource(R.drawable.sensor_icon);

		String name = (String) groupList.get(groupPosition).get("name");
		txt_name.setText(name);
		String count = (String) groupList.get(groupPosition).get("count");
		txt_count.setText("[" + count + "]");

		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.lv_all_device_item, null);
		}
		toolbarGrid = (MyGridView) convertView
				.findViewById(R.id.gv_all_device_item);
		HashMap<String, Object> deviceMap = groupList.get(groupPosition);
		int category = (Integer) deviceMap.get("category");
		 List<Device> deviceList = childList.get(groupPosition);
		toolbarGrid.setAdapter(getAdapter(mActivity, deviceList, category));
		return convertView;
	}

	public BaseAdapter getAdapter(Activity mActivity, List<Device> deviceList,
			int category) {
		if (category == light) {
			LightGridViewAdapter lightAdapter = new LightGridViewAdapter(
					mActivity, deviceList, R.layout.light_contrl_item);
			return lightAdapter;
		} else if (category == curtain) {
			CurtainGridViewAdapter curtainAdapter = new CurtainGridViewAdapter(
					mActivity, deviceList, R.layout.curtain_dimmable_item);
			return curtainAdapter;
		} else if (category == climate) {
			ClimateListViewAdapter climateAdapter = new ClimateListViewAdapter(
					mActivity, deviceList, R.layout.lv_climate_control_item);
			return climateAdapter;
		} else if (category == lock) {
			LockGridViewAdapte lockAdapter = new LockGridViewAdapte(mActivity,
					deviceList, R.layout.gv_lock_control_item);
			return lockAdapter;
		} else if (category == sensors) {
			SensorsGridViewAdapte sensorsAdapter = new SensorsGridViewAdapte(
					mActivity, deviceList, R.layout.gv_sensors_control_item);
			return sensorsAdapter;
		} else
			return null;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return false;
	}
}
